package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.ExpansionPanel;
import javafx.scene.Node;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class ExpansionPanelActions {
    static{
        PropertyFactory.regist(ExpansionPanel.class, "expandedProperty", o -> {
            ExpansionPanel obj = (ExpansionPanel) o;
            return obj.expandedProperty();
        });
        PropertyFactory.regist(ExpansionPanel.class, "collapsedContentProperty", o -> {
            ExpansionPanel obj = (ExpansionPanel) o;
            return obj.collapsedContentProperty();
        });
        PropertyFactory.regist(ExpansionPanel.class, "expandedContentProperty", o -> {
            ExpansionPanel obj = (ExpansionPanel) o;
            return obj.expandedContentProperty();
        });
    }

    public static void init(ExpansionPanel node, Thing thing, ActionContext actionContext) {
        ControlActions.init(node, thing, actionContext);

        Node collapsedContent = JavaFXUtils.getObject(thing, "collapsedContent", actionContext);
        if (collapsedContent != null) {
            node.setCollapsedContent(collapsedContent);
        }

        Node expandedContent = JavaFXUtils.getObject(thing, "expandedContent", actionContext);
        if (expandedContent != null) {
            node.setExpandedContent(expandedContent);
        }

        if (thing.valueExists("expanded")) {
            node.setExpanded(thing.getBoolean("expanded"));
        }

    }

    public static ExpansionPanel create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        ExpansionPanel node = new ExpansionPanel();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createCollapsedContent(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ExpansionPanel parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setCollapsedContent((Node) obj);
                break;
            }
        }
    }

    public static void createExpandedContent(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ExpansionPanel parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setExpandedContent((Node) obj);
                break;
            }
        }
    }
}